﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CanBo_TinHoc : UserControl
    {
        string temp = "";
        string temp1 = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CanBo_TinHoc()
        {
            InitializeComponent();
        }

        private void UC_CanBo_TinHoc_Load(object sender, EventArgs e)
        {
            Load_CB_TH();

            //Hiển thị tùy chọn tin học lên combobox
            

        }
        public void Load_CB_TH()
        {
            dataGridView_TH.DataSource = sv.getCB_TH();
            btn_Luu_TH.Enabled = false;
            btn_Huy_TH.Enabled = false;


            cmb_MaTH_TH.DataSource = sv.getTH();
            cmb_MaTH_TH.DisplayMember = "DienGiai";
            cmb_MaTH_TH.ValueMember = "Ma_TH";

            cmb_XepLoai_TH.DataSource = sv.getCB_TH();
            cmb_XepLoai_TH.DisplayMember = "XepLoai";
            cmb_XepLoai_TH.ValueMember = "Ma_CB";
            cmb_XepLoai_TH.ValueMember = "Ma_TH";

            cmb_MaCB_TH.DataSource = sv.getCB_TH();
            cmb_MaCB_TH.DisplayMember = "Ma_CB";
            cmb_MaCB_TH.ValueMember = "Ma_CB";
        }

        private void btn_ThemMaTH_Click(object sender, EventArgs e)
        {

            frm_TIN_HOC themmatinhoc = new frm_TIN_HOC();
            themmatinhoc.ShowDialog();
        }

        private void btn_ThemMaCB_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView_TH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cmb_MaCB_TH.Text = dataGridView_TH.CurrentRow.Cells[0].Value.ToString();
            cmb_MaTH_TH.Text = dataGridView_TH.CurrentRow.Cells[1].Value.ToString();
            date_NgayCap_TH.Text = dataGridView_TH.CurrentRow.Cells[2].Value.ToString();
            txt_NoiCap_TH.Text = dataGridView_TH.CurrentRow.Cells[3].Value.ToString();
            cmb_XepLoai_TH.Text = dataGridView_TH.CurrentRow.Cells[4].Value.ToString();
            temp = dataGridView_TH.CurrentRow.Cells[1].Value.ToString();
            temp1 = dataGridView_TH.CurrentRow.Cells[0].Value.ToString();
            cmb_MaCB_TH.Enabled = false;
            cmb_MaTH_TH.Enabled = false;
            cmb_XepLoai_TH.Enabled = false;
            date_NgayCap_TH.Enabled = false;
            txt_NoiCap_TH.Enabled = false;
        }

        private void btn_Them_TH_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Them_TH.Enabled = true;
            btn_Luu_TH.Enabled = true;
            btn_Huy_TH.Enabled = true;
            btb_Xoa_TH.Enabled = false;
            btn_Sua_TH.Enabled = false;
            cmb_MaCB_TH.Enabled = true;
            cmb_MaTH_TH.Enabled = true;
            cmb_XepLoai_TH.Enabled = true;
            date_NgayCap_TH.Enabled = true;
            txt_NoiCap_TH.Enabled = true;
            cmb_MaCB_TH.Text = "";
            cmb_MaTH_TH.Text = "";
            date_NgayCap_TH.Text = "";
            txt_NoiCap_TH.Text = "";
            cmb_XepLoai_TH.Text = "";

        }

        private void btn_Luu_TH_Click(object sender, EventArgs e)
        {
            btn_Luu_TH.Enabled = true;
            btb_Xoa_TH.Enabled = true;
            btn_Sua_TH.Enabled = true;


            string a = cmb_MaCB_TH.SelectedValue.ToString();
            string b = cmb_MaTH_TH.SelectedValue.ToString();
            string c = Convert.ToString(date_NgayCap_TH.Text);
            string d = txt_NoiCap_TH.Text;
            string f = cmb_XepLoai_TH.SelectedValue.ToString(); ;

            if (a == "" | b == "" | c == "" | d == "" | f == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {

                ServiceReference1.CANBO_TINHOC cbth = new ServiceReference1.CANBO_TINHOC();
                cbth.Ma_CB = a;
                cbth.Ma_TH = b;
                cbth.NgayCap = DateTime.Parse(c);
                cbth.NoiCap = d;
                cbth.XepLoai = f;
                int kq = 0;
                if (flag == "them")
                {
                    if(sv.check_CB_TH(cbth))
                        kq = -1;
                    else
                    sv.addCB_TH(cbth);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setCB_TH(cbth);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_CB_TH();
                    btb_Xoa_TH.Enabled = true;
                    btn_Sua_TH.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_TH_Click(object sender, EventArgs e)
        {
            btn_Luu_TH.Enabled = false;
            btn_Huy_TH.Enabled = false;
            btb_Xoa_TH.Enabled = true;
            btn_Sua_TH.Enabled = true;
        }

        private void btb_Xoa_TH_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_TINHOC cbth = new ServiceReference1.CANBO_TINHOC();
            cbth.Ma_TH = temp;
            cbth.Ma_CB = temp1;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dữ liệu này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCB_TH(cbth);
                Load_CB_TH();
                MessageBox.Show("Đã xóa!");
                cmb_MaCB_TH.Text = "";
                cmb_MaTH_TH.Text = "";
                date_NgayCap_TH.Text = "";
                txt_NoiCap_TH.Text = "";
                cmb_XepLoai_TH.Text = "";
            }
        }

        private void btn_Sua_TH_Click(object sender, EventArgs e)
        {
            flag = "sua";
            cmb_MaCB_TH.Enabled = false;
            cmb_MaTH_TH.Enabled = false;
            date_NgayCap_TH.Enabled = true;
            txt_NoiCap_TH.Enabled = true;
            cmb_XepLoai_TH.Enabled = true;
            btn_Luu_TH.Enabled = true;
        }

        private void cmb_MaTH_TH_SelectedValueChanged(object sender, EventArgs e)
        {

        }

        private void cmb_MaCB_TH_SelectedValueChanged(object sender, EventArgs e)
        {

        }
    }
}
